package itheima03;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;

/**
 * @author xxx
 * @date 2020/12/26 22:06
 * @description
 */
public class Test3 {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket=new ServerSocket(7777);

        //一直开启服务器端
        while (true){
            Socket socket=serverSocket.accept();


            /*Test.method(socket,true);*/
            new Thread(new Runnable() {
                @Override
                public void run() {
                   try {
                     String wjm=System.currentTimeMillis()+"_"+new Random().nextInt()+".jpg";
                     FileOutputStream fileOutputStream=new FileOutputStream(new File("D:\\java349\\day14\\upload",wjm));

                     InputStream inputStream=socket.getInputStream();

                     int b=0;
                     byte[] bytes=new byte[1024];
                     while ((b=inputStream.read(bytes))!=-1){
                         fileOutputStream.write(bytes,0,b);
                     }
                   OutputStream outputStream=socket.getOutputStream();
                    outputStream.write("上传成功".getBytes());

                    /*serverSocket.close();*/
                    socket.close();
                    fileOutputStream.close();
                    inputStream.close();
                    outputStream.close();
                   }  catch (IOException e) {
                       e.printStackTrace();
                   }
                }
            }).start();
        }
    }
}
